package com.jed.xxh.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.jed.xxh.R;
import com.jed.xxh.utils.L;

import java.util.ArrayList;
import java.util.List;

/**
 * 项目名：MyApplication<br/>
 * 包名：  com.jed.xxh.ui<br/>
 * 作者：徐晓辉<br/>
 * 创建日期：2018/11/24 17:55<br/>
 * 描述：引导页<br/>
 */
public class GuideActivity extends AppCompatActivity implements View.OnClickListener {

    private ViewPager mViewPager;
    //容器：三个引导页面
    private List<View> mList = new ArrayList<>();
    private View view1,view2,view3;

    //小圆点
    private ImageView point1,point2,point3;

    //跳过
    private ImageView iv_back;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_guide);

        initView();
    }

    /**
     * 初始化
     */
    private void initView() {

        //初始化跳过按钮并设置点击事件
        iv_back = findViewById(R.id.iv_back);
        iv_back.setOnClickListener(GuideActivity.this);

        //初始化三个引导页面
        mViewPager = findViewById(R.id.mViewPager);
        view1 = View.inflate(GuideActivity.this,R.layout.pager_item_one,null);
        view2 = View.inflate(GuideActivity.this,R.layout.pager_item_two,null);
        view3 = View.inflate(GuideActivity.this,R.layout.pager_item_three,null);
        view3.findViewById(R.id.btn_start).setOnClickListener(GuideActivity.this);
        mList.add(view1);
        mList.add(view2);
        mList.add(view3);
        //设置适配器
        mViewPager.setAdapter(new GuideAdapter());

        //初始化是三个圆点并设置圆点的选中状态
        point1 = findViewById(R.id.point1);
        point2 = findViewById(R.id.point2);
        point3 = findViewById(R.id.point3);
        setPointImage(true,false,false);

        //监听引导页滑动
        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }
            //page切换
            @Override
            public void onPageSelected(int position) {
                L.i("position："+position);
                switch (position){
                    case 0:
                        setPointImage(true,false,false);
                        iv_back.setVisibility(View.VISIBLE);
                        break;
                    case 1:
                        setPointImage(false,true,false);
                        iv_back.setVisibility(View.VISIBLE);
                        break;
                    case 2:
                        setPointImage(false,false,true);
                        iv_back.setVisibility(View.GONE);
                        break;
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });



    }

    /**
     * 设置小圆点的选中状态和效果
     */
    private void setPointImage(boolean isCheck1,boolean isCheck2,boolean isCheck3) {
        if (isCheck1){
            point1.setBackgroundResource(R.drawable.point_on);
        }else{
            point1.setBackgroundResource(R.drawable.point_off);
        }

        if (isCheck2){
            point2.setBackgroundResource(R.drawable.point_on);
        }else{
            point2.setBackgroundResource(R.drawable.point_off);
        }

        if (isCheck3){
            point3.setBackgroundResource(R.drawable.point_on);
        }else{
            point3.setBackgroundResource(R.drawable.point_off);
        }
    }

    /**
     * 引导页的点击事件
     */
    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.btn_start:
                /*startActivity(new Intent(GuideActivity.this, LoginActivity.class));
                finish();
                break;*/
            case R.id.iv_back:
                startActivity(new Intent(GuideActivity.this, LoginActivity.class));
                finish();
                break;
        }
    }

    private class GuideAdapter extends PagerAdapter {
        @Override
        public int getCount() {
            return mList.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            container.addView(mList.get(position));
            return mList.get(position);
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView(mList.get(position));
        }
    }
}
